<%
metamodel http://www.eclipse.org/uml2/2.0.0/UML
import org.acceleo.modules.uml2.services.Uml2Services
import org.acceleo.modules.uml2.services.StringServices
%>
<%script type="Class" name="taglibPackage"%>
org.taam.test.taglib

<%script type="Class" name="className"%>
EntityFormTag

<%script type="Class" name="fullFilePath"%>
/<%taglibPackage.toPath()%>/<%className%>.java


<%script type="Class" name="generate" file="<%fullFilePath%>"%>

package <%taglibPackage%>;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.taam.test.utility.Utility;

public class EntityFormTag extends TagSupport {
 
 	/**
     * Commons Logging instance.
     */
	private static final Log log = LogFactory.getLog(<%className%>.class);

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2618866108919171087L;


	private String entityName;

	private HashMap<String, String> formSet=new HashMap<String, String>();
	

	
	@Override
	public int doStartTag() throws JspException {
		log.debug("demarre EntityTag");
		JspWriter out= pageContext.getOut();
		try {	//TODO optimize this		
			out.print("<form action=\""+pageContext.getServletContext().getContextPath()+"/"+Utility.firstLower(getEntityName())+"Control\" method=\"post\">");
			out.print(getForm(getEntityName()));
				out.print("</form>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;

	}

	private String getForm(String entityName) {
		FileReader fr;String retour="";
				
		String repertoire=pageContext.getServletContext().getRealPath("/");
		System.out.println(repertoire);
		String tmp = formSet.get(entityName);
		if (tmp== null)

			try {
				fr=new FileReader(new File(repertoire+"/WEB-INF/forms/"+entityName+"Form.html"));
				BufferedReader br=new BufferedReader(fr);
				StringBuffer sb=new StringBuffer("");String ligne;
				while ((ligne = br.readLine()) != null)
					sb.append(ligne);
				tmp=sb.toString();
				formSet.put(entityName,tmp);				
				
			} catch (FileNotFoundException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
				
				e.printStackTrace();
				
			}
			
				retour=tmp;	
		
		return retour;
	}

	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}



}